/**
 * Created by abiu on 2016-06-22.
 */
var urls = {};
var expecteds = {};
var models = {
    //type:default string
    channel:{
        "channelId":{require:true,unique:true},
        "channelName":{required:true,unique:true},
        "contactPerson":{},
        "phone":{},
        "address":{}
    },
    menu: {
        "menuId": {type: 'number', required: true, unique: true},
        "name": {required: true, unique: true},
        "englishName": {required: true, unique: true},
        "parentId": {
            type: function (result,dataM) {
                dataM.menu.get(function (dataResult) {
                    result.parentId = dataResult._id;
                }, 'allMenuUse', 'parent')
            }
        },
        "template": {required: true},
        "icon": {required: true}

    },
    role: {
        "name": {required: true, unique: true},
        "note": {},
        "menus": {
            type: function (result,dataM) {
                dataM.menu.get(function (data) {
                    result.parentId = data.result._id;
                }, 'allMenuUse', 'parent')
            }
        }
    }
};

function initGet(key,dataM,result){
    var dataTypes = {
        channel:{},
        menu:{parent:{change:{},remove:['parentId']}}
    }


    dataM[key]['get'] = function(cb,index,dataType){
        if(cb ==undefined )
        throw 'get() require @cb ';
        index = index || 'defaultIndex';
        //默认数据类型为模型定义的数据样式
        dataType = dataType || 'normal';
        dataTypes[key]['normal'] = {};
        result[key][dataType] = result[key][dataType] ||{};
        var dataInType = result[key][dataType];
        if(dataInType[index]){
            it('get '+key+'.'+dataType+'.'+index,function(done){
                cb(dataInType[index]);
                expect(dataInType[index]['_id']).not.toBeUndefined()
                done();
            })
        }

        else{
            dataInType[index] = {};//先声明这个数据已存在
            var modelInType = dataTypes[key][dataType];
            if(!modelInType)
            throw 'unsupported dataType' + dataType;
            function callback(data){
                if(data.code==0)
                dataInType[index] = data.result;
                cb(data.result);
            }
            dataM[key]['create'](callback,null,null,modelInType['change'],modelInType['remove']);
        }

    }
}

